package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.box.BoxVcbState;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.BoxFunctionEnum;
import com.ecube.utils.EnumUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建箱变实体
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/7/31 14:11
 */
public class SendBoxVcbState {

    public static void main(String[] args) {

        // 创建箱变
        sendRealDataMQTT("FAT", "100050145", "TMS002025005");
    }

    public static void sendRealDataMQTT(String env, String productCode, String deviceCode){
        BoxVcbState vcbState = BoxVcbState.init();
        BoxFunctionEnum function = (BoxFunctionEnum) EnumUtils.getFunction(vcbState.getClass());
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DataReportStandard reportStandard = new DataReportStandard();
        reportStandard.setHeader(ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction()));

        reportStandard.setDataBody(vcbState);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
        String time = dateFormat.format(date);
        MqttUtils.sendMQTT(env, productCode + "/" + deviceCode + "/S2M/keepalive", time);
    }

}
